import axios from '@/axios.config'

export default {
    state: {
        emailInfo:{},
        basicInfo:{},
        dataLink: [],
        msg: '',
        flag: 0,
        totleNum: 0,
        verifyCode: 0,
        connectType:[],
        sqlType:[],
        taskStatus:[],
        localDB: {},
        logLevels: {},
        colors:"",
        userColor: "#e06733",
        systemTitle:"",
        loginBg:"",
        bookImg: "",
        zgImg:"",
        linsence_code: 0,
        remainDay: 0
    },
    mutations: {
        send_msg(state, payload) {
            state.msg = payload._msg,
            state.flag = payload._flag
        },
        add_info(state, payload) {
            state.emailInfo = Object.assign({},payload.com_info);
            state.verifyCode = payload.com_code
        },
        add_basic(state, payload) {
            state.basicInfo = Object.assign({},payload.com_basic)
        },
        add_linklist(state, payload) {
            state.dataLink = [].concat(payload.com_linklist);
            state.totleNum = payload.com_totle;
        },
        add_verify(state, payload) {
            state.verifyCode = payload.com_code
        },
        add_types(state, payload) {
            state.connectType = [].concat(payload.com_connecttype);
            state.sqlType = [].concat(payload.com_sqltype);
            state.taskStatus = [].concat(payload.com_taskStatus);
        },
        add_localDB(state, payload) {
            state.localDB = Object.assign({}, payload.com_localdb);
        },
        add_levels(state, payload) {
            state.logLevels = Object.assign({}, payload.com_levels);
        },
        change_color(state, payload) {
            state.colors = payload.colors
        },
        change_usercolor(state, payload) {
            if(payload.colors !== null) {
                state.userColor = payload.colors;
                state.bookImg = require("../../assets/images/book_"+payload.colors+".png");
                state.zgImg = require("../../assets/images/zg_"+payload.colors+".png");
            }
        },
        add_title(state, payload) {
            state.systemTitle = payload.com_title,
            state.loginBg = payload.com_loginBg
        },
        add_linsencecode(state, payload) {
            state.linsence_code = payload.linsence_code,
            state.remainDay = payload.linsence_remainDay
        }
    },
    actions: {
        //邮箱设置接口
        saveEmail({ commit }, payload) {
            return axios.post('/api/email/update',payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: '保存成功'
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        getInfo({ commit }, payload) {
            return axios.post('/api/email/info', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_info', {
                            com_info: res.data.data,
                            com_code: res.data.retCode
                        })
                        commit('add_verify',{
                            com_code:res.data.retCode
                        })
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }else if(res.data.retCode == 9999995){
                        commit('add_verify',{
                            com_code:res.data.retCode
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //发送测试邮件
        sendEmail({ commit }, payload) {
            return axios.post('/api/email/send', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "发送测试邮件成功，请检查邮箱是否收到测试邮件"
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    } 
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //基本设置接口
        saveBasicInfo({ commit }, payload) {
            return axios.post('/api/common/update', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "设置成功"
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        getBasicInfo({ commit }, payload) {
            return axios.post('/api/common/info', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_basic', {
                            com_basic: res.data.data
                        })
                        commit('add_verify',{
                            com_code:res.data.retCode
                        })
                    }else if(res.data.retCode == 9999995){
                        commit('add_verify',{
                            com_code:res.data.retCode
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    } 
                })
                .catch((error) => {
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //数据链接设置接口
        getLinkList({ commit }, payload) {
            return axios.post('/api/auth/connection/list', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_linklist', {
                            com_linklist: res.data.data.t,
                            com_totle: res.data.data.totalCount
                        })
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //新建数据库链接
        createDataLink({ commit }, payload) {
            return axios.post('/api/auth/connection/save', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "保存成功",
                            _flag:0
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                            _flag:1
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                        _flag:1
                    })
                })
        },
        updateDataLink({ commit }, payload) {
            return axios.post('/api/connect/update', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "修改成功",
                            _flag: 0
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                            _flag: 1
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                        _flag: 1
                    })
                })
        },
        delDataLink({ commit }, payload) {
            return axios.post('/api/auth/connection/batchDel', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "删除成功",
                            _flag: 0
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                            _flag: 1
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                        _flag: 1
                    })
                })
        },
        //获取数据库驱动
        getTypes({ commit },payload) {
            return axios.post('/api/common/config', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_types', {
                            com_connecttype: res.data.data.connectTypes,
                            com_sqltype: res.data.data.sqlTypes,
                            com_taskStatus: res.data.data.taskStatusConstantList
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //获取日志控制级别
        getLevels({ commit },payload) {
            return axios.post('/api/logger/levels', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_levels', {
                            com_levels: res.data.data,
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error
                    })
                })
        },
        //测试链接
        testLink({ commit },payload) {
            return axios.post('/api/connect/test', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                            _flag: 0
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                            _flag:1
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                        _flag:1
                    })
                })
        },
        //重启服务
        restartServe({ commit }, payload) {
            return axios.post('/api/system/restart', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                    })
                }) 
        },
        //获取本地数据库
        getLocaleDB({ commit }, payload) {
            return axios.post('/api/common/currentDb', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_localDB', {
                            com_localdb: res.data.data,
                        })
                    }else if(res.data.retCode == 9999995){
                        commit('add_verify',{
                            com_code:res.data.retCode
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                    })
                }) 
        },
        //更新本地数据库
        updateLocaleDB({ commit }, payload) {
            return axios.post('/api/common/updateBaseDb', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "更新成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                    })
                }) 
        },
        //上移下移
        moveDataLink({ commit }, payload) {
            return axios.post('/api/auth/connection/move', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "移动成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                    })
                }) 
        },
        //获取颜色
        getColor({ commit }, payload) {
            return axios.post('/api/auth/user/color', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('change_color', {
                            colors: res.data.data,
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                    })
                }) 
        },
        //获取用户颜色
        getUserColor({ commit }, payload) {
            return axios.post('/api/auth/user/dashboardColor', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('change_usercolor', {
                            colors: res.data.data,
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                    })
                }) 
        },
        //更新颜色
        updateColor({ commit }, payload) {
            return axios.post('/api/auth/user/updateColor', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "更新成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                    })
                }) 
        },
        //更新用户颜色
        updateUserColor({ commit }, payload) {
            return axios.post('/api/auth/user/updateDashboardColor', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('send_msg', {
                            _msg: "更新成功",
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                    })
                }) 
        },
        //获取系统标题 
        getSysTitle({ commit }, payload) {
            return axios.post('/api/auth/common/info', payload)
                .then((res) => {
                    if(res.data.retCode == 0) {
                        commit('add_title', {
                            com_title: res.data.data.title,
                            com_loginBg: res.data.data.loginBg
                        })
                    }else{
                        commit('send_msg', {
                            _msg: res.data.retMsg,
                        })
                    }
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                    })
                }) 
        },
        //测试
        testLinsence({ commit },payload) {
            return axios.post('/api/system/valid', payload)
                .then((res) => {
                    commit('add_linsencecode', {
                        linsence_code: res.data.retCode,
                        linsence_remainDay: res.data.data.remainDay
                    })
                })
                .catch((error) =>　{
                    commit('send_msg', {
                        _msg: error,
                        _flag:1
                    })
                })
        },
    }
}